﻿using System;
using System.Text;
using System.Windows.Forms;

namespace DevLav
{
    public partial class Form1 : Form
    {
        int steps = 0;
        int x = 13, y = 56;
        int second=0,minute = 0;
        Button[,] buttons = new Button[4, 4];

        public Form1()
        {
            InitializeComponent();
            this.обИгреToolStripMenuItem.Click += обИгреToolStripMenuItem_Click;
            Start();
        }

        void обИгреToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormAbout form = new FormAbout(this);
            form.ShowDialog();
        }

        public void Start()
        {
            int count = 1;
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    Button button1 = new System.Windows.Forms.Button();
                    button1.Location = new System.Drawing.Point(x, y);
                    button1.Name = "button1";
                    button1.FlatStyle = FlatStyle.Flat;
                    button1.Size = new System.Drawing.Size(46, 42);
                    button1.TabIndex = 1;
                    if (count == 16)
                        button1.Text = " ";
                    else
                        button1.Text = count.ToString();
                    count++;
                    Visible = true;
                    button1.UseVisualStyleBackColor = true;
                    this.Controls.Add(button1);
                    button1.Click += new System.EventHandler(this.ButtonNumber_Click);
                    buttons[i, j] = button1;
                    x += 50;
                }
                x = 13;
                y += 50;
            }

            for (int i = 0; i < 4; i++)
                for (int j = 0; j < 4; j++)
                    buttons[i, j].Enabled = false;
        }

        private void ButtonNumber_Click(object sender, EventArgs e)
        {
            Button obj = (Button) sender;
            for (int i = 0; i < 4; i++)
                for (int j = 0; j < 4; j++)
                    if (obj == buttons[i, j])
                    {
                        Check(i, j);
                        if (CheckWon())
                        {
                            GameTimer.Stop();
                            EndGame();
                            return;
                        }
                    }      
        }

        private void EndGame()
        {
            if (second < 10) MessageBox.Show("Вы выиграли!\r\nВремя игры : " + minute + ":0" + second+"\r\n"+"Количество ходов :"+steps);
            else             MessageBox.Show("Вы выиграли!\r\nВремя игры : " + minute + ":" + second + "\r\n" +"Количество ходов :" + steps);
            minute = 0;
            second = 0;
            TimerTextBox.Text = "0:00";
            StepsTextBox.Text = "0";

            for (int i = 0; i < 4; i++)
                for (int j = 0; j < 4; j++)
                    buttons[i, j].Enabled = false;
            buttonPause.Enabled = false;
            паузаToolStripMenuItem.Enabled = false;
        }

        private void Check(int i, int j)
        {

            if (i == 0 && j == 0)
            {
                if (buttons[i, j + 1].Text == " ")
                {
                    right(i, j);
                    return;
                }
                if (buttons[i + 1, j].Text == " ")
                {
                    bottom(i, j);
                    return;
                }
            }
            else if (i == 0 && j == 3)
            {
                if (buttons[i, j - 1].Text == " ")
                {
                    left(i, j);
                    return;
                }
                if (buttons[i + 1, j].Text == " ")
                {
                    bottom(i, j);
                    return;
                }
            }

            else if (i == 3 && j == 0)
            {
                if (buttons[i - 1, j].Text == " ")
                {
                    top(i, j);
                    return;
                }
                if (buttons[i, j + 1].Text == " ")
                {
                    right(i, j);
                    return;
                }
            }
            else if (i == 3 && j == 3)
            {
                if (buttons[i, j - 1].Text == " ")
                {
                    left(i, j);
                    return;
                }
                if (buttons[i - 1, j].Text == " ")
                {
                    top(i, j);
                    return;
                }

            }
            else if (i == 0 && j < 3 && j > 0)
            {
                if (buttons[i, j + 1].Text == " ")
                {
                    right(i, j);
                    return;
                }
                if (buttons[i, j - 1].Text == " ")
                {
                    left(i, j);
                    return;
                }
                if (buttons[i + 1, j].Text == " ")
                {
                    bottom(i, j);
                    return;
                }
            }
            else if (i == 3 && j < 3 && j > 0)
            {
                if (buttons[i, j + 1].Text == " ")
                {
                    right(i, j);
                    return;
                }
                if (buttons[i, j - 1].Text == " ")
                {
                    left(i, j);
                    return;
                }
                if (buttons[i - 1, j].Text == " ")
                {
                    top(i, j);
                    return;
                }
            }

            else if (i < 3 && i > 0 && j == 0)
            {
                if (buttons[i + 1, j].Text == " ")
                {
                    bottom(i, j);
                    return;
                }
                if (buttons[i - 1, j].Text == " ")
                {
                    top(i, j);
                    return;
                }
                if (buttons[i, j + 1].Text == " ")
                {
                    right(i, j);
                    return;
                }
            }
            else if (i < 3 && i > 0 && j == 3)
            {
                if (buttons[i + 1, j].Text == " ")
                {
                    bottom(i, j);
                    return;
                }
                if (buttons[i - 1, j].Text == " ")
                {
                    top(i, j);
                    return;
                }
                if (buttons[i, j - 1].Text == " ")
                {
                    left(i, j);
                    return;
                }
            }

            else
            {
                if (buttons[i + 1, j].Text == " ")
                {
                    bottom(i, j);
                    return;
                }
                if (buttons[i - 1, j].Text == " ")
                {
                    top(i, j);
                    return;
                }
                if (buttons[i, j + 1].Text == " ")
                {
                    right(i, j);
                    return;
                }
                if (buttons[i, j - 1].Text == " ")
                {
                    left(i, j);
                    return;
                }

            }
            

        }

        private void top(int i, int j)
        {
            buttons[i - 1, j].Text = buttons[i, j].Text;
            buttons[i, j].Text = " ";
            steps++;
            StepsTextBox.Text = "" + steps;
        }

        private void left(int i, int j)
        {
            buttons[i, j - 1].Text = buttons[i, j].Text;
            buttons[i, j].Text = " ";
            steps++;
            StepsTextBox.Text = "" + steps;
        }

        private void right(int i, int j)
        {
            buttons[i, j + 1].Text = buttons[i, j].Text;
            buttons[i, j].Text = " ";
            steps++;
            StepsTextBox.Text = "" + steps;
        }

        private void bottom(int i, int j)
        {
            buttons[i + 1, j].Text = buttons[i, j].Text;
            buttons[i, j].Text = " ";
            steps++;
            StepsTextBox.Text = "" + steps;
        }

        private void Begin()
        {
            Random rnd = new Random((int)DateTime.Now.Ticks);
            for (int i = 0; i < 100; i++)
                Check(rnd.Next(0,4), rnd.Next(0,4));
            steps = 0;
            StepsTextBox.Text = "" + steps;
            TimerTextBox.Text = "0:00";
        }

        private bool CheckWon()
        {
            int count = 1;
            for (int i = 0; i < 4; i++)
                for (int j = 0; j < 4; j++)
                {
                    if (count != 16 && buttons[i, j].Text != count.ToString())
                        return false;
                    count++;
                }
            return true;
        }

        private void ButtonStart_Click(object sender, EventArgs e)
        {
            GameTimer.Start();
            Begin();
            паузаToolStripMenuItem.Enabled = true;
            buttonPause.Enabled = true;

            for (int i = 0; i < 4; i++)
                for (int j = 0; j < 4; j++)
                    buttons[i, j].Enabled = true;
            buttonPause.Enabled = true;
            паузаToolStripMenuItem.Enabled = true;
            buttonPause.Text = "Пауза";
            паузаToolStripMenuItem.Text = "Пауза";
        }

        private void buttonPause_Click(object sender, EventArgs e)
        {
            if (buttonPause.Text == "Пауза")
            {
                buttonPause.Text = "Продолжить";
                паузаToolStripMenuItem.Text = "Продолжить";
                for (int i = 0; i < 4; i++)
                    for (int j = 0; j < 4; j++)
                        buttons[i, j].Enabled = false;
                GameTimer.Stop();
            }
            else if (buttonPause.Text == "Продолжить")
            {
                buttonPause.Text = "Пауза";
                паузаToolStripMenuItem.Text = "Пауза";
                for (int i = 0; i < 4; i++)
                    for (int j = 0; j < 4; j++)
                        buttons[i, j].Enabled = true;
                GameTimer.Start();
            }
        }

        private void выходToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult res = MessageBox.Show("Вы уверенны, что хотите закрыть игру?", 
                "Закритие", MessageBoxButtons.YesNo);
            if (res == DialogResult.Yes)
                Close();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult res = MessageBox.Show("Вы уверенны, что хотите закрыть игру?",
               "Закритие", MessageBoxButtons.YesNo);
            if (res == DialogResult.No)
                e.Cancel = true;
        }

        private void рекордыToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            FormRecords record = new FormRecords();
            record.ShowDialog();
        }

        private void GameTimer_Tick(object sender, EventArgs e)
        {
            while (second >= 60)
            {
                minute++;
                second -= 60;
            }
            if (second < 10) TimerTextBox.Text = "" + minute + ":0" + second;
            else             TimerTextBox.Text = "" + minute + ":" + second;

            second++;
        }

    }
}
